﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace HeathCareSystem
{
    public class ChanDoanBenhDAO
    {
        static DataProvider dp = new DataProvider();
        protected static List<ChanDoanBenhDTO> TaoDanhSach(String strSQL)
        {
            List<ChanDoanBenhDTO> lst = new List<ChanDoanBenhDTO>();
            try
            {
                DataTable dt = new DataTable();
                dt = dp.ExecuteQuery(strSQL);
                foreach (DataRow dr in dt.Rows)
                {
                    ChanDoanBenhDTO ctba = new ChanDoanBenhDTO();
                    ctba.maChiTietBenhAn = int.Parse(dr["MaChiTietBenhAn"].ToString());
                    ctba.maBenh = int.Parse(dr["MaBenh"].ToString());
                    lst.Add(ctba);
                }
            }
            catch (Exception)
            {
                lst = null;
            }
            return lst;
        }
        public static bool Them(ChanDoanBenhDTO cdb)
        {
            bool kq = false;
            try
            {
                string strSQL = string.Format("INSERT INTO ChanDoanBenh(MaChiTietBenhAn, MaBenh) VALUES ({0}, {1})", cdb.maChiTietBenhAn, cdb.maBenh);
                dp.ExecuteNonQuery(strSQL);
                kq = true;
            }
            catch (Exception)
            {
            }
            return kq;
        }

        public static bool KiemTraTonTai(int ma)
        {
            string strSQL = string.Format("SELECT COUNT(MaChiTietBenhAn) AS COUNT FROM ChanDoanBenh " +
            " WHERE (MaChiTietBenhAn = {0})", ma);
            int kq = int.Parse(dp.ExecuteScalar(strSQL).ToString());
            return kq > 0;
        }

        public static bool CapNhat(ChanDoanBenhDTO cdb)
        {
            try
            {
                String strSQL = String.Format("UPDATE ChanDoanBenh SET MaBenh = '{0}' WHERE MaChiTietBenhAn = {1}"
                                                , cdb.maBenh, cdb.maChiTietBenhAn);
                dp.ExecuteNonQuery(strSQL);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static ChanDoanBenhDTO LayThongTinTheoMa(int ma)
        {
            string strSQL = "SELECT * FROM ChanDoanBenh WHERE MaChiTietBenhAn = '" + ma + "'";
            List<ChanDoanBenhDTO> lst = TaoDanhSach(strSQL);
            if (lst.Count == 0)
                return null;
            else
                return TaoDanhSach(strSQL)[0];
        }
    }
}